using System;
using System.Diagnostics;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using Debug = UnityEngine.Debug;

public class BuildApk : IPreprocessBuildWithReport
{
    public int callbackOrder => 0;


    [MenuItem("BuildTool/Build_apk")]
    static void Build()
    {
        string[] scenes = GetScenePaths(); // 获取所有场景的路径

        string outputPath = "Build/"; // Android输出路径
        string appName = Application.productName + "_v" + Application.version + "(" + PlayerSettings.Android.bundleVersionCode + ").apk"; // 应用程序名称
        EditorUserBuildSettings.buildAppBundle = false;

        var result = BuildPipeline.BuildPlayer(scenes, outputPath + appName, BuildTarget.Android, BuildOptions.CompressWithLz4);


        if (result.summary.result == BuildResult.Succeeded)
        {
            Debug.Log("apk构建成功:" + result.summary.outputPath);
            Process.Start(outputPath);
        }
        else
        {
            Debug.LogError("apk构建失败!可以检查一下gradleTemplate文件的unityStreamingAssets=.unity3d*STREAMING_ASSETS*是否多了个*");
        }
    }

    [MenuItem("BuildTool/Build_aab")]
    public static void BuildAAB()
    {
        string[] scenes = GetScenePaths(); // 获取所有场景的路径

        string outputPath = "Build/"; // Android输出路径
        string appName = Application.productName + "_v" + Application.version + "(" + PlayerSettings.Android.bundleVersionCode + ").aab"; // 应用程序名称
        EditorUserBuildSettings.buildAppBundle = true;
        var result = BuildPipeline.BuildPlayer(scenes, outputPath + appName, BuildTarget.Android, BuildOptions.CompressWithLz4);

        if (result.summary.result == BuildResult.Succeeded)
        {
            Debug.Log("aab构建成功:" + result.summary.outputPath);
            Process.Start(outputPath);
        }
        else
        {
            Debug.LogError("aab构建失败!可以检查一下gradleTemplate文件的unityStreamingAssets=.unity3d*STREAMING_ASSETS*是否多了个*");
        }
    }

    // 获取所有场景的路径
    static string[] GetScenePaths()
    {
        EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes;
        string[] scenePaths = new string[scenes.Length];

        for (int i = 0; i < scenes.Length; i++)
        {
            scenePaths[i] = scenes[i].path;
        }

        return scenePaths;
    }

    public void OnPreprocessBuild(BuildReport report)
    {
#if UNITY_ANDROID
        if (CheckForNamespaceAndClass("HWGames", "AdManager"))
        {
            if (SystemInfo.deviceUniqueIdentifier != "F6AEE8A7-5CCF-5DC0-98F9-2EC391A98E14" && SystemInfo.deviceUniqueIdentifier != "f740874a7a2624687e8ad17c52b6610dcb19d08e")
            {
                throw new BuildFailedException("导入SDK后只能在正式打包机进行打包");
            }
        }
#endif
    }

    // 检查是否存在指定命名空间和类名的脚本
    private bool CheckForNamespaceAndClass(string namespaceName, string className)
    {
        // 获取所有程序集
        var assemblies = AppDomain.CurrentDomain.GetAssemblies();

        foreach (var assembly in assemblies)
        {
            // 获取程序集中的所有类型
            var types = assembly.GetTypes();

            foreach (var type in types)
            {
                // 检查命名空间和类名是否匹配
                if (type.Namespace == namespaceName && type.Name == className)
                {
                    return true;
                }
            }
        }

        return false;
    }
}
